package com.mooc.house.service;

import com.mooc.house.common.PageParams;
import com.mooc.house.model.House;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class RecommendService {

    private static final String HOT_HOUSE_KEY = "hot_house";
    private static final Logger logger = LoggerFactory.getLogger(RecommendService.class);

    @Autowired
    private HouseService houseService;

    @Autowired
    private StringRedisTemplate redisTemplate;

   public void increase(Long id){
       redisTemplate.opsForZSet().incrementScore(HOT_HOUSE_KEY, ""+id, 1.0D);
       redisTemplate.opsForZSet().removeRange(HOT_HOUSE_KEY, 0, -11);
   }


   public List<House> getHotHouse(Integer size){
       Set<String> idSet = redisTemplate.opsForZSet().reverseRange(HOT_HOUSE_KEY, 0, -1);
       //bug修复，根据热度从高到低
       List<Long> ids = idSet.stream().map(b -> Long.parseLong(b)).collect(Collectors.toList());
       House house = new House();
       house.setIds(ids);
       PageParams pageParams = new PageParams();
       pageParams.setLimit(size);
       pageParams.setOffset(0);
       return houseService.queryAndSetImg(house,pageParams);

   }



}
